$(function () {
	// 表单验证
	$("form").on("submit", function (event) {
		event.preventDefault(); // 阻止表单默认提交

    var username = document.querySelector('.username').value;
    var password = document.querySelector('.password').value;
    var rpassword = document.querySelector('.rpassword').value;
    var nickname = document.querySelector('.nickname').value;
		console.log(password, rpassword);
		if (!username || !password || !rpassword || !nickname) {
			$(".error").text("请填写所有必填项").show();
			return false;
		}
		if (password !== rpassword) {
			$(".error").text("两次密码不一致").show();
			return false;
		}

		axios
			.post("http://localhost:9000/users/register", {
				username: username,
				password: password,
				rpassword: rpassword,
				nickname: nickname,
			})
			.then(function (response) {
				if (response.data.code === 1) {
					$(".error").text(response.data.message).show();
					window.location.href = "./login.html";
				} else {
					$(".error").text(response.data.message).show();
				}
			})
			.catch(function (error) {
				alert("请求出错");
			});
	});
});
